﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MidiPlayer
{
    public partial class MidiPlayer : Form
    {
        private MidiPlayerSettings _settings = new MidiPlayerSettings();

        private AudioPlayer _player = new AudioPlayer();

        public MidiPlayer()
        {
            _settings = MidiPlayerSettings.LoadSettings(MidiPlayerSettings.GetDefaultFilename());

            InitializeComponent();
            UpdateView();
        }

        private void OnSelectFile(object sender, EventArgs e)
        {
            var fDialog = new OpenFileDialog();
            fDialog.Title = "Select MP3 or WAV file";
            fDialog.Filter = "Supported Files (*.wav, *.mp3)|*.wav;*.mp3";
            fDialog.CheckFileExists = true; 
            if (!string.IsNullOrEmpty(_settings.PlaybackFile))
            {
                fDialog.InitialDirectory = Path.GetDirectoryName(_settings.PlaybackFile);
            }


            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                _settings.PlaybackFile = fDialog.FileName;
            }
            UpdateView();
        }

        private void UpdateView()
        {
            lblFile.Text = String.IsNullOrEmpty(_settings.PlaybackFile)
                               ? "Nothing"
                               : Path.GetFileName(_settings.PlaybackFile);

            lblOut.Text = _settings.PlaySettings.GetDisplaySettings();

            lblMidi.Text = _settings.MidiSettings.GetDisplaySettings();

            btnPlay.Enabled = !_player.IsPlaying() && !_player.IsMidiPlaying();
            btnMidi.Enabled = !_player.IsPlaying() && !_player.IsMidiPlaying();
            btnStop.Enabled = _player.IsPlaying() || _player.IsMidiPlaying();
        }

        private void MidiPlayer_Load(object sender, EventArgs e)
        {

        }

        private void OnCloseApp(object sender, FormClosingEventArgs e)
        {
            _player.StopMidi();
            _player.Close();
            _settings.SaveSettings(MidiPlayerSettings.GetDefaultFilename());
        }

        private void btnCfgPlay_Click(object sender, EventArgs e)
        {
            var form = new PlaybackSettingsForm(_settings.PlaySettings);
            if(form.ShowDialog(this) == DialogResult.OK)
            {
                _settings.PlaySettings = form.Settings;
            }
            UpdateView();

        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            try
            {
                _settings.Validate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, 
                    "Settings validation error", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Stop);
                return;
            }

            _player.Settings = _settings;
            _player.StartPlayFile();

            UpdateView();

        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            _player.StopMidi();
            _player.StopPlayFile();

            UpdateView();
        }

        private void btnCfgMidi_Click(object sender, EventArgs e)
        {
            var form = new MidiSettingsForm(_settings.MidiSettings);
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                _settings.MidiSettings = form.Settings;
            }
            UpdateView();

        }

        private void btnDoMidi_Click(object sender, EventArgs e)
        {
            try
            {
                _settings.Validate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message,
                    "Settings validation error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Stop);
                return;
            }
            _player.Settings = _settings;
            _player.StartMidi();

            UpdateView();
        }

      
    }
}
